ext2_fs_open (Ext2Fs *fs, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "name", "flags", "superblock",
- "block_size", NULL };
+ "block_size", "offset", NULL };
char * name;
- int flags = 0, superblock = 0, err;
+ int flags = 0, superblock = 0, offset = 0, err;
unsigned int block_size = 0;
ext2_filsys efs;
+ char offsetopt[30];
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iii", kwlist,
- &name, &flags, &superblock, &block_size))
- return NULL;
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist,
+ &name, &flags, &superblock,
+ &block_size, &offset))
+ return NULL;
if (fs->fs != NULL) {
PyErr_SetString(PyExc_ValueError, "already have an fs object");
return NULL;
}
- err = ext2fs_open(name, flags, superblock, block_size,
+ if (offset != 0) {
+ snprintf(offsetopt, 29, "offset=%d", offset);
+ }
+
+ err = ext2fs_open2(name, offsetopt, flags, superblock, block_size,
unix_io_manager, &efs);
if (err) {
PyErr_SetString(PyExc_ValueError, "unable to open file");
ext2_fs_new(PyObject *o, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "name", "flags", "superblock",
- "block_size", NULL };
+ "block_size", "offset", NULL };
char * name;
- int flags = 0, superblock = 0;
+ int flags = 0, superblock = 0, offset;
unsigned int block_size = 0;
Ext2Fs *pfs;
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iii", kwlist,
- &name, &flags, &superblock, &block_size))
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist,
+ &name, &flags, &superblock, &block_size,
+ &offset))
return NULL;
pfs = (Ext2Fs *) PyObject_NEW(Ext2Fs, &Ext2FsType);
pfs->fs = NULL;
if (!ext2_fs_open(pfs,
- Py_BuildValue("siii", name, flags, superblock, block_size),
- NULL))
+ Py_BuildValue("siiii", name, flags, superblock,
+ block_size, offset), NULL))
return NULL;
return (PyObject *)pfs;
import grub.GrubConf
import grub.fsys
-PYGRUB_VER = 0.02
+PYGRUB_VER = 0.3
def draw_window():
buf = os.read(fd, 512)
os.close(fd)
- if len(buf) >= 512 and struct.unpack("H", buf[0x1fe: 0x200]) == (0xaaff):
+ if len(buf) >= 512 and struct.unpack("H", buf[0x1fe: 0x200]) == (0xaa55,):
return True
return False
+SECTOR_SIZE=512
+def get_active_offset(file):
+ """Find the offset for the start of the first active partition in the
+ disk image file."""
+ fd = os.open(file, os.O_RDONLY)
+ buf = os.read(fd, 512)
+ for poff in (446, 462, 478, 494): # partition offsets
+ # active partition has 0x80 as the first byte
+ if struct.unpack("<c", buf[p:p+1]) == ('\x80',):
+ return struct.unpack("<", buf[p+8:p+12])[0] * SECTOR_SIZE
+ return -1
+
def get_config(fn):
if not os.access(fn, os.R_OK):
raise RuntimeError, "Unable to access %s" %(fn,)
cf = grub.GrubConf.GrubConfigFile()
+ offset = 0
if is_disk_image(fn):
- raise RuntimeError, "appears to be a full disk image... unable to handle this yet"
+ offset = get_active_offset(fn)
+ if offset == -1:
+ raise RuntimeError, "Unable to find active partition on disk"
# open the image and read the grub config
fs = None
for fstype in grub.fsys.fstypes.values():
- if fstype.sniff_magic(fn):
- fs = fstype.open_fs(fn)
+ if fstype.sniff_magic(fn, offset):
+ fs = fstype.open_fs(fn, offset)
break
if fs is not None:
if img.initrd:
print " initrd: %s" %(img.initrd[1],)
+ offset = 0
if is_disk_image(file):
- raise RuntimeError, "unable to handle full disk images yet"
+ offset = get_active_offset(fn)
+ if offset == -1:
+ raise RuntimeError, "Unable to find active partition on disk"
# read the kernel and initrd onto the hostfs
fs = None
for fstype in grub.fsys.fstypes.values():
- if fstype.sniff_magic(file):
- fs = fstype.open_fs(file)
+ if fstype.sniff_magic(file, offset):
+ fs = fstype.open_fs(file, offset)
break
if fs is None: